﻿using System;
using System.Drawing;
using System.Windows.Forms;
using Semantic;

namespace WindowsFormsApplication1
{
    public partial class Chat : Form
    {
        private SemanticNetwork _semanticNetwork;
        private FormMain _parent;

        public delegate void ShowExplain(Explanation explanation);
        public event ShowExplain showExplain;
        public void ExpShow(Explanation explanation)
        {
            if (showExplain != null) showExplain(explanation);
        }

        public Chat(SemanticNetwork sn, FormMain parent)
        {
            InitializeComponent();
            _semanticNetwork = sn;
            _parent = parent;
        }

        private void btn_enter_Click(object sender, EventArgs e)
        {
            DoAnswer();
        }
        
        private void box_question_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter) DoAnswer();
        }

        /// <summary>
        /// Вывод по сем.сети
        /// </summary>
        private void DoAnswer()
        {
            var question = box_question.Text.Trim();
            if(question.Length==0)
            {
                MessageBox.Show(@"Задайте пожалуйста вопрос!");
                return;
            }
            var cons = new Consultation(question, _semanticNetwork);
            var res = cons.Conclude(_parent._Settings);
            if (res != ErrorMsg.Ok) MessageBox.Show(cons.ExplainRoute.ShowMessage(res));
            else
            {
                var questItem = new ListViewItem(question);
                questItem.Font = new Font(questItem.Font, FontStyle.Bold);
                list_history.Items.Add(questItem);
                var ansItem = new ListViewItem(cons.Answer);
                ansItem.Font = new Font(ansItem.Font, FontStyle.Italic);
                list_history.Items.Add(ansItem);
                ansItem.Selected = true;
                ansItem.EnsureVisible();
                //вызов метода основной формы для рисования объяснения вывода
                showExplain += _parent.ShowExplaination;
                showExplain += _parent.ShowExplaination;
                showExplain += _parent.ShowExplaination;
                ExpShow(cons.ExplainRoute);
                showExplain -= _parent.ShowExplaination;
                showExplain -= _parent.ShowExplaination;
                showExplain -= _parent.ShowExplaination;
            }
        }
    }
}
